package com.huifaapp.mall.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.SPUtils;
import com.google.gson.Gson;
import com.huifaapp.mall.R;
import com.huifaapp.mall.adapter.tmall_teamoneAdapter;
import com.huifaapp.mall.afinal.SPConstants;
import com.huifaapp.mall.api.ApiManager;
import com.huifaapp.mall.bean.GetMyteamHeadBean;
import com.huifaapp.mall.uitils.AUniqueIdentifier;
import com.squareup.okhttp.Request;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by duanyu on 2018/1/5.
 * 团队列表
 */

public class Tmall_shopkeeperFragment_team extends AppCompatActivity {

    private TabLayout tmall_shopeeper_team_tabLayout;
    private ViewPager tmall_shopeeper_team_viewPager;
    private List<String>Teamlist=new ArrayList<>();
    private  String getMyteamHeadURL= ApiManager.BASE_NIUPAI_URL+"users/getMyteamHead";
    private String myteamOne;
    private String myteamTwo;
    private LinearLayout tmall_shopkeepeper_team_btn;
    private GetMyteamHeadBean getMyteamHeadBean;
    private TextView activity_tmll_detail_teamNum;
    private ImageView activity_tmll_detail_return;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tmall_shopkeeperfragment_team);
        activity_tmll_detail_return= (ImageView) findViewById(R.id.activity_tmll_detail_return);
        activity_tmll_detail_return.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        initView();
        initHttp();




    }

    private void initHttp() {
        Map<String,String>map=new HashMap<>();
        map.put("token", SPUtils.getInstance().getString(SPConstants.KEY_USER_TOKEN));
        map.put("appId", AUniqueIdentifier.getDeviceId(Tmall_shopkeeperFragment_team.this));
        Log.e("团队头接口",getMyteamHeadURL+"?token="+SPUtils.getInstance().getString(SPConstants.KEY_USER_TOKEN)+"&appId="+AUniqueIdentifier.getDeviceId(Tmall_shopkeeperFragment_team.this));
        OkHttpUtils.post().params(map).url(getMyteamHeadURL).build().execute(new StringCallback() {
            @Override
            public void onError(Request request, Exception e) {

            }

            @Override
            public void onResponse(String response) {
                Gson gson=new Gson();
                getMyteamHeadBean = gson.fromJson(response, GetMyteamHeadBean.class);
                if (getMyteamHeadBean.getCode().equals("10000")){
                    try {
                        myteamOne = getMyteamHeadBean.getMyteamOne();
                        myteamTwo = getMyteamHeadBean.getMyteamTwo();
                    }catch (NullPointerException e){
                        myteamOne="0";
                        myteamTwo="0";
                    }
                    Teamlist.add("一级好友"+"("+myteamOne+")");
                    Teamlist.add("二级好友"+"("+myteamTwo+")");
                    tmall_teamoneAdapter tmall_teamoneAdapter=new tmall_teamoneAdapter(getSupportFragmentManager(),Teamlist);
                    tmall_shopeeper_team_viewPager.setAdapter(tmall_teamoneAdapter);
                    tmall_shopeeper_team_tabLayout.setupWithViewPager(tmall_shopeeper_team_viewPager);
                    activity_tmll_detail_teamNum.setText("("+getMyteamHeadBean.getInvitedCount()+")");
                }


            }
        });
    }

    private void initView() {
        tmall_shopeeper_team_tabLayout = (TabLayout) findViewById(R.id.tmall_shopeeper_team_TabLayout);
        tmall_shopeeper_team_viewPager = (ViewPager) findViewById(R.id.tmall_shopeeper_team_ViewPager);
        tmall_shopkeepeper_team_btn = (LinearLayout)findViewById(R.id.tmall_shopkeepeper_team_btn);
        activity_tmll_detail_teamNum = (TextView) findViewById(R.id.activity_tmll_detail_teamNum);
        tmall_shopkeepeper_team_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(Tmall_shopkeeperFragment_team.this,PublicCountActivity.class);
                startActivity(intent);
            }
        });
    }
}
